package com.zxx.order.ui.activity

import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.zqq.baselibrary.ext.onClick
import com.zqq.baselibrary.ui.activity.BaseMvpActivity
import com.zqq.provider.router.RouterPath
import com.zxx.order.R
import com.zxx.order.common.OrderConstant
import com.zxx.order.injection.component.DaggerShipAddressComponent
import com.zxx.order.injection.module.ShipAddressModule
import com.zxx.order.presenter.EditShipAddressPresenter
import com.zxx.order.presenter.iview.EditShipAddressView
import com.zxx.order.service.bean.ShipAddress
import kotlinx.android.synthetic.main.activity_ship_address_edit.*
import org.jetbrains.anko.toast

/**
 * 收货人编辑页面
 * wc
 */
@Route(path = RouterPath.OrderCenter.PATH_SHIPEDIT_ADDRESS)
class ShipAddressEditActivity : BaseMvpActivity<EditShipAddressPresenter>(), EditShipAddressView {

    private var mAddress: ShipAddress? = null

    override fun injectComponent() {
        DaggerShipAddressComponent.builder()
                .activityComponent(mActivityComponent)
                .shipAddressModule(ShipAddressModule())
                .build()
                .inject(this)
        mPresenter.mView = this
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ship_address_edit)

        initView()
        initData()
    }

    //初始化视图
    private fun initView() {
        mSaveBtn.onClick {
            if (mShipNameEt.text.isNullOrEmpty()) {
                toast("名称不能为空")
                return@onClick
            }
            if (mShipMobileEt.text.isNullOrEmpty()) {
                toast("电话不能为空")
                return@onClick
            }
            if (mShipAddressEt.text.isNullOrEmpty()) {
                toast("电话不能为空")
                return@onClick
            }
            if (mAddress == null) {
                //新增
                mPresenter.addShipAddress(mShipNameEt.text.toString(),
                        mShipMobileEt.text.toString(),
                        mShipAddressEt.text.toString())
            } else {
                //编辑
                mAddress!!.shipUserName = mShipNameEt.text.toString()
                mAddress!!.shipUserMobile = mShipMobileEt.text.toString()
                mAddress!!.shipAddress = mShipAddressEt.text.toString()

                mPresenter.editShipAddress(mAddress!!)
            }

        }


    }

    //初始化编辑数据
    private fun initData() {
        mAddress = intent.getParcelableExtra(OrderConstant.KEY_SHIP_ADDRESS)

        mAddress?.let {
            mShipNameEt.setText(it.shipUserName)
            mShipMobileEt.setText(it.shipUserMobile)
            mShipAddressEt.setText(it.shipAddress)
        }
    }

    //------接口返回----------
    override fun onAddShipAddressResult(result: Boolean) {
        toast("添加地址成功")
        finish()
    }

    override fun onEditShipAddressResult(result: Boolean) {
        toast("修改地址成功")
        finish()
    }

}
